
package com.yuan.dialog;

import com.yuan.adapt.AdaptListUtils;
import com.yuan.adapt.RadioAdapter;
import com.yuan.adapt.ResolveListAdapter;
import com.yuan.adapt.WeekCheckBoxAdapt;
import com.yuan.control.CheckControl;
import com.yuan.control.SettingControl;
import com.yuan.graduate.R;
import com.yuan.localServer.LocalManage;
import com.yuan.model.M上班考勤表;
import com.yuan.model.M下班考勤表;
import com.yuan.model.M请假申请;
import com.yuan.prefence.Constants;
import com.yuan.prefence.FileConstants;
import com.yuan.prefence.MsgKeys;
import com.yuan.prefence.PreferenceKeys;
import com.yuan.prefence.UrlConstants;
import com.yuan.unit.PreferenceUtil;
import com.yuan.unit.TimeUtils;
import com.yuan.unit.ToastHelper;

import org.json.JSONException;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

import java.util.List;
import java.util.Map;

/**
 * Dialog工具类 生成Dialog都在这个类
 * 
 * @author YUAN
 */
public class DialogUtil implements PreferenceKeys, Constants, UrlConstants, FileConstants {
    public static CustomDialog createCheckInDialog(final Context context, final Handler mHandler) {
        CustomDialog dialog = new CustomDialog(context);
        dialog.setTitle(R.string.check_in_title);
        String isChecked = "";
        dialog.addContentView(R.layout.checkdialog);
        final EditText e_reason = (EditText) dialog.findViewById(R.id.e_reason);
        TextView t_reason = (TextView) dialog.findViewById(R.id.t_reason);
        if (!CheckControl.isCheckedInToday())
        {
            isChecked = "今天上班尚未签到  \n";
            String str = TimeUtils.getNowTime();
            if (CheckControl.isRightChechinTime(str))
            {
                e_reason.setVisibility(View.GONE);
            }
            String adress = LocalManage.getInstance().adress;
            str = String.format(
                    context.getResources().getString(R.string.check_in_msg),
                    str, adress);

            str = isChecked + str;
            t_reason.setText(str);
            dialog.setPositiveButton(R.string.ensure, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                    if (e_reason.getVisibility() == View.VISIBLE)
                    {
                        String reason = e_reason.getText().toString().trim();
                        if (TextUtils.isEmpty(reason)) {
                            ToastHelper.getInstance().shortToast(context, "请填写原因");
                        } else
                        {
                            M上班考勤表.getInstance().set备注("上班打卡异常，理由：" + reason);
                            CheckControl.setCheckIn(context, mHandler);
                            dialog.dismiss();
                        }
                    }
                    else {
                        CheckControl.setCheckIn(context, mHandler);
                        dialog.dismiss();
                    }
                }
            });
            dialog.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                    dialog.dismiss();
                }
            });
        }
        else
        {
            isChecked = "今天上班已经签到  不可重复签到  \n";
            String str = M上班考勤表.getInstance().get日期();
            e_reason.setVisibility(View.GONE);
            String adress = M上班考勤表.getInstance().get位置();
            str = String.format(
                    context.getResources().getString(R.string.check_in_msg),
                    str, adress);
            str = isChecked + str;
            t_reason.setText(str);
            dialog.setNeutralButton(R.string.cancel, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                    dialog.dismiss();
                }
            });
        }
        return dialog;
    }

    public static CustomDialog createCheckOutDialog(final Context context, final Handler mHandler) {
        CustomDialog dialog = new CustomDialog(context);
        dialog.setTitle(R.string.check_out_title);
        String isChecked = "";
        dialog.addContentView(R.layout.checkdialog);
        final EditText e_reason = (EditText) dialog.findViewById(R.id.e_reason);
        TextView t_reason = (TextView) dialog.findViewById(R.id.t_reason);
        if (CheckControl.isRightChechoutTime(TimeUtils.getNowTime()))
        {
            e_reason.setVisibility(View.GONE);
        }
        if (!CheckControl.isCheckedOutToday())
        {
            isChecked = "今天下班尚未签到\n";
            String str = TimeUtils.getNowTime();
            str = String.format(
                    context.getResources().getString(R.string.check_out_msg),
                    str, LocalManage.getInstance().adress);
            str = isChecked + str;
            t_reason.setText(str);
            dialog.setPositiveButton(R.string.ensure, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub

                    if (e_reason.getVisibility() == View.VISIBLE)
                    {
                        String reason = e_reason.getText().toString().trim();
                        if (TextUtils.isEmpty(reason)) {
                            ToastHelper.getInstance().shortToast(context, "请填写原因");
                        } else
                        {
                            M下班考勤表.getInstance().set备注("下班打卡异常，理由：" + reason);
                            CheckControl.setCheckOut(context, mHandler);
                            dialog.dismiss();
                        }
                    }
                    else {
                        CheckControl.setCheckOut(context, mHandler);
                        dialog.dismiss();
                    }
                }
            });
            dialog.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                    dialog.dismiss();
                }
            });
        }
        else
        {
            e_reason.setVisibility(View.GONE);
            isChecked = "今天下班已经签到    不可重复签到  \n";
            String str = M下班考勤表.getInstance().get日期();
            String adress = M下班考勤表.getInstance().get位置();
            str = String.format(
                    context.getResources().getString(R.string.check_out_msg),
                    str, adress);
            str = isChecked + str;
            t_reason.setText(str);
            dialog.setNeutralButton(R.string.cancel, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                    dialog.dismiss();
                }
            });
        }

        return dialog;
    }

    public static CustomDialog createChangeComnfigDialog(Context context) throws JSONException
    {
        final CustomDialog dialog = new CustomDialog(context);
        dialog.setTitle(R.string.choice_site);
        ListView listview = new ListView(context);
        final List<Map<String, Object>> list = AdaptListUtils.getInstance().getSystemConfig();
        Adapter listItemAdapter = new RadioAdapter(context, list.size(), list, "companyname",
                SettingControl.getCompanyName());
        listview.setAdapter((ListAdapter) listItemAdapter);
        dialog.addContentView(listview);
        dialog.setNeutralButton(R.string.cancel, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                dialog.dismiss();
            }
        });
        listview.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // TODO Auto-generated method stub
                SettingControl.setCompanyName((String) list.get(position).get("companyname"));
                SettingControl.setCompanyUrl((String) list.get(position).get("url"));
                dialog.dismiss();
            }
        });
        return dialog;
    }

    public static final CustomDialog createFullAppShareDialog(final Context context) {
        final CustomDialog dialog = new CustomDialog(context);
        dialog.setTitle(R.string.share);
        dialog.addContentView(R.layout.share_other_dialog);
        GridView gridView = (GridView) dialog.findViewById(R.id.share_container);
        gridView.setAdapter(new ResolveListAdapter(context, buildOnlySendTextIntent(), null, null));
        gridView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                dialog.dismiss();
                ResolveListAdapter adapter = (ResolveListAdapter) parent.getAdapter();
                Intent intentForPosition = adapter.intentForPosition(position);
                context.startActivity(intentForPosition);
            }
        });
        dialog.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        return dialog;
    }

    private static Intent buildOnlySendTextIntent() {
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("text/plain");
        intent.putExtra(Intent.EXTRA_SUBJECT, "毕业设计分享分享");
        intent.putExtra(Intent.EXTRA_TEXT, "袁伟的毕业设计不错哦~ \n PS:来自袁伟的毕业设计");
        return intent;
    }

    public static final CustomDialog createCommonDialog(final Context context, String title,
            String msg) {
        final CustomDialog dialog = new CustomDialog(context);
        dialog.setTitle(title);
        dialog.setMessage(msg);
        dialog.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        return dialog;
    }

    public static final CustomDialog createEnsureDialog(final Context context, String title,
            String msg, DialogInterface.OnClickListener ClickListener) {
        final CustomDialog dialog = new CustomDialog(context);
        dialog.setTitle(title);
        dialog.setMessage(msg);
        dialog.setPositiveButton(R.string.ensure, ClickListener);
        dialog.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        return dialog;
    }

    public static final CustomDialog createResetConfigDialog(final Context context, String title,
            String msg, DialogInterface.OnClickListener listen) {
        final CustomDialog dialog = new CustomDialog(context);
        dialog.setTitle(title);
        dialog.setMessage(msg);
        dialog.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        dialog.setPositiveButton(R.string.ensure, listen);
        return dialog;

    }

    public static final CustomDialog createStartWorkTimeInputDialog(final Context context,
            final Handler mHandler) {
        final CustomDialog dialog = new CustomDialog(context);
        dialog.addContentView(R.layout.custom_dialog_clock);
        dialog.setTitle(R.string.set_chechin_time);
        final EditText et_time = (EditText) dialog.findViewById(R.id.et_time);
        dialog.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        dialog.setPositiveButton(R.string.ensure, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                String str = et_time.getText().toString().trim();
                if (CheckControl.isRightStartWorkTime(str))
                {
                    SettingControl.SetStartWorkTime(str);
                    dialog.dismiss();
                    mHandler.sendEmptyMessage(MsgKeys.MAINSETTINGREFRESH);
                    ToastHelper.getInstance().shortToast(context, R.string.setting_succeed);
                }
                else
                {
                    ToastHelper.getInstance().shortToast(context, R.string.setting_failed);
                    et_time.setText("");
                }
            }
        });
        return dialog;
    }

    public static final CustomDialog createEndWorkTimeInputDialog(final Context context,
            final Handler mHandler) {
        final CustomDialog dialog = new CustomDialog(context);
        dialog.addContentView(R.layout.custom_dialog_clock);
        dialog.setTitle(R.string.set_chechout_time);
        final EditText et_time = (EditText) dialog.findViewById(R.id.et_time);
        dialog.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        dialog.setPositiveButton(R.string.ensure, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                String str = et_time.getText().toString().trim();
                if (CheckControl.isRightStartWorkTime(str))
                {
                    SettingControl.SetEndWorkTime(str);
                    dialog.dismiss();
                    mHandler.sendEmptyMessage(MsgKeys.MAINSETTINGREFRESH);
                    ToastHelper.getInstance().shortToast(context, R.string.setting_succeed);
                }
                else
                {
                    ToastHelper.getInstance().shortToast(context, R.string.setting_failed);
                    et_time.setText("");
                }
            }
        });
        return dialog;
    }

    public static final CustomDialog createCityChoiceDialog(final Context context,
            final Handler mHandler, String str) {
        final CustomDialog dialog = new CustomDialog(context);
        dialog.setTitle(R.string.choice_search_city);
        ListView listview = new ListView(context);
        final List<Map<String, Object>> list = AdaptListUtils.getInstance().getChinaCity();
        Adapter listItemAdapter = new RadioAdapter(context, list.size(), list, "city", str);
        listview.setAdapter((ListAdapter) listItemAdapter);
        dialog.addContentView(listview);
        dialog.setNeutralButton(R.string.cancel, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                dialog.dismiss();
            }
        });
        listview.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // TODO Auto-generated method stub
                Message msg = new Message();
                msg.what = MsgKeys.CITYCHOICE;
                Bundle bundle = new Bundle();
                String city = (String) list.get(position).get("city");
                bundle.putString("city", city);
                msg.setData(bundle);
                mHandler.sendMessage(msg);
                dialog.dismiss();
            }
        });
        return dialog;
    }

    public static final CustomDialog createWorkAdressDialog(final Context context,
            final String str, final String lat, final String lng)
    {
        final CustomDialog dialog = new CustomDialog(context);
        dialog.setTitle(R.string.set_work_adress_title);
        String tempstr = String.format(
                context.getResources().getString(R.string.set_work_adress_msg),
                str);
        dialog.setMessage(tempstr);
        dialog.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                dialog.dismiss();
            }
        });
        dialog.setPositiveButton(R.string.ensure, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                SettingControl.SetWorkAdress(str);
                SettingControl.SetWorkLat(lat);
                SettingControl.SetWorkLng(lng);
                ToastHelper.getInstance().shortToast(context, R.string.setting_succeed);
                dialog.dismiss();
            }
        });
        return dialog;
    }

    public static final CustomDialog createWeekDialog(final Context context) {
        final CustomDialog dialog = new CustomDialog(context);
        dialog.setTitle(R.string.set_week);
        ListView listView = new ListView(context);
        Adapter mWeekCheckBoxAdapt = new WeekCheckBoxAdapt(context);
        listView.setAdapter((ListAdapter) mWeekCheckBoxAdapt);
        dialog.addContentView(listView);
        dialog.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                dialog.dismiss();
            }
        });
        dialog.setPositiveButton(R.string.ensure, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                ToastHelper.getInstance().shortToast(context, R.string.setting_succeed);
                dialog.dismiss();
            }
        });
        listView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // TODO Auto-generated method stub
                boolean flag = PreferenceUtil.getInstance().getBoolean(WEEK[position], false);
                PreferenceUtil.getInstance().putBoolean(WEEK[position], !flag);
            }
        });
        return dialog;
    }

    public static final CustomDialog createOffLineMapDialog(final Context context,
            final Handler mHandler, String city)
    {
        final CustomDialog dialog = new CustomDialog(context);
        dialog.setTitle(R.string.off_line_map_title);
        String str = String.format(
                context.getResources().getString(R.string.off_line_map_msg),
                city);
        dialog.setMessage(str);
        dialog.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                PreferenceUtil.getInstance().putBoolean(AGREEN_TO_LOAD_MAP, false);
                dialog.dismiss();
            }
        });
        dialog.setPositiveButton(R.string.ensure, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                dialog.dismiss();
                mHandler.sendEmptyMessage(MsgKeys.LOADMAP);
            }
        });
        return dialog;
    }

    public static final CustomDialog createLeaveTypeDialog(final Context context,
            final Handler mHandler, final List<?> list)
    {
        final CustomDialog dialog = new CustomDialog(context);
        dialog.setTitle(R.string.choice_search_city);
        ListView listview = new ListView(context);
        Adapter listItemAdapter = new RadioAdapter(context, list.size(), list, MARK.MARK_VALUE,
                null);
        listview.setAdapter((ListAdapter) listItemAdapter);
        dialog.addContentView(listview);
        dialog.setNeutralButton(R.string.cancel, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                dialog.dismiss();
            }
        });
        listview.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // TODO Auto-generated method stub
                Message msg = new Message();
                msg.what = MsgKeys.LEAVETYPE;
                M请假申请.getInstance().set请假类别(
                        (String) ((Map<?, ?>) list.get(position)).get(MARK.MARK_MARK));
                String str = (String) ((Map<?, ?>) list.get(position)).get(MARK.MARK_VALUE);
                msg.obj = str;
                mHandler.sendMessage(msg);
                dialog.dismiss();
            }
        });
        return dialog;
    }

    public static final CustomDialog createPhotoDialog(final Context context,
            final Handler mHandler, final ListView mlistview) {
        final CustomDialog dialog = new CustomDialog(context);
        dialog.setTitle(R.string.choice_photo_type);
        dialog.addContentView(mlistview);
        dialog.setNeutralButton(R.string.cancel, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                dialog.dismiss();
            }
        });
        return dialog;
    }
}
